﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login_admin : System.Web.UI.Page
{
    AdminManager manaAdmin = new AdminManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["diaoc_admin_do_online"] != null)
        {
            Response.Redirect("admin.aspx");
        }
        HttpCookie c = Request.Cookies["userInfodools"];
        if (c != null && !string.IsNullOrEmpty(Server.HtmlEncode(Request.Cookies["userInfodools"]["userdools"])))
        {
            TBUsername.Text = Server.HtmlEncode(Request.Cookies["userInfodools"]["userdools"]);
            TBPassword.Text = Server.HtmlEncode(Request.Cookies["userInfodools"]["passdools"]);
            loginadmin(TBUsername.Text, TBPassword.Text, CBRemember.Checked);
        }
    }

    private void loginadmin(string username, string password, bool remember)
    {
        Admin admin = new Admin();
        admin.Username = username;
        string pass = CommonFuns.HashSHA1(username + password);
        admin.Password = pass;

        if (manaAdmin.check_admin_login(admin))
        {
            Session["diaoc_admin_do_online"] = username;
            if (remember)
            {
                Response.Cookies["userInfodools"]["userdools"] = username;
                Response.Cookies["userInfodools"]["passdools"] = password;
                Response.Cookies["userInfodools"].Expires = DateTime.Now.AddMinutes(21600);
            }
            else
            {
                Response.Cookies["userInfodools"].Expires = DateTime.Now.AddMinutes(0);
            }

            Response.Redirect("admin.aspx");
        }
        else
        {
            LMessageError.Visible = true;
        }
    }
    protected void BLogin_Click(object sender, EventArgs e)
    {
        loginadmin(TBUsername.Text.Trim(), TBPassword.Text, CBRemember.Checked);
    }
}